package instances
{
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2FixtureDef;
	
	import noiseandheat.ane.box2d.Box2D;
	import noiseandheat.ane.box2d.data.b2BodyProxy;

	public class ExtPolygonInstance extends ExtBox2DInstance
	{
		protected var instWidth:Number;
		protected var instHeight:Number;
		
		public function ExtPolygonInstance()
		{
			super();
		}
		public function createBoxBody(pBox2D:Box2D, pScale:Number,x:Number,y:Number,
									  width:Number,height:Number,rotation:Number,
									  density:Number = 2.0, restitution:Number = .1, friction:Number = .1,
									  isDynamic:Boolean = false):b2BodyProxy{
			
			b2d = pBox2D;
			scale = pScale;
			
			
			
			
			var bodyDef:b2BodyDef = new b2BodyDef();
			bodyDef.type = isDynamic ? b2Body.b2_dynamicBody : b2Body.b2_staticBody;
			
			bodyDef.position.Set(x/scale, y/scale);
			bodyDef.angle = rotation * Math.PI / 180;
			
			body = b2d.createBody(bodyDef);
			
			
			fixtureDef = new b2FixtureDef();
			fixtureDef.density = density;
			fixtureDef.restitution = restitution;
			fixtureDef.friction = friction;
			
			
			b2d.createBodyFixtureWithBoxShape(body.id, width/scale,height/scale,fixtureDef);
			
			instWidth = width;
			instHeight = height;
			
			return body;
		}
		
		override public function updateBody(model:Object):void{
			b2d.destroyBody(body.id);
			createBoxBody(b2d,scale,model.x,model.y,model.width/2,model.height/2,
				model.rotation,model.density,model.restitution,model.friction,model.isDynamic);
		}
		
		/*override public function pack():XMLList{
			var xmlList:XMLList = super.pack();
			xmlList.p.@w = XMLList(instWidth);
			xmlList.p.@h = XMLList(instHeight);
			
			return xmlList;
		}*/
	}
}